cmake_minimum_required(VERSION 3.0.2)
project(color_correction)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  sensor_msgs
  cv_bridge
  image_transport
  message_generation
)

## Find OpenCV
find_package(OpenCV REQUIRED)

## Find yaml-cpp
find_package(PkgConfig REQUIRED)
pkg_check_modules(YAML_CPP REQUIRED yaml-cpp)
if(NOT YAML_CPP_FOUND)
    # Try to find yaml-cpp without pkg-config
    find_package(yaml-cpp REQUIRED)
    set(YAML_CPP_LIBRARIES yaml-cpp)
endif()

## Generate services in the 'srv' folder
add_service_files(
  FILES
  CalibrateColorCorrection.srv
  ApplyColorCorrection.srv
)

## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  std_msgs
  sensor_msgs
)

###################################
## catkin specific configuration ##
###################################
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME}
  CATKIN_DEPENDS roscpp rospy std_msgs sensor_msgs cv_bridge image_transport message_runtime
  DEPENDS OpenCV
)

###########
## Build ##
###########

## Specify additional locations of header files
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  "/home/firefly/Software/json/include"
)

## Create core color correction library
add_library(${PROJECT_NAME}
  src/color_correction.cpp
)

## Add cmake target dependencies of the library
add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
target_link_libraries(${PROJECT_NAME}
  ${catkin_LIBRARIES}
  ${OpenCV_LIBS}
)

## Create MCU communication library
add_library(mcu_communication
  src/mcu_correspond/mcu_data_sender.cpp
  src/mcu_correspond/mcu_data_subscriber.cpp
)

target_link_libraries(mcu_communication
  ${catkin_LIBRARIES}
  ${YAML_CPP_LIBRARIES}
)

## Main color correction node (with MCU integration)
add_executable(color_correction_node src/color_correction_node.cpp)
add_dependencies(color_correction_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(color_correction_node
  ${PROJECT_NAME}
  mcu_communication
  ${catkin_LIBRARIES}
  ${OpenCV_LIBS}
)

## Color correction processor (dedicated image processing)
add_executable(color_correction_processor src/color_correction_processor.cpp)
add_dependencies(color_correction_processor ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(color_correction_processor
  ${PROJECT_NAME}
  ${catkin_LIBRARIES}
  ${OpenCV_LIBS}
)

## C++ client tool
add_executable(color_correction_client src/color_correction_client.cpp)
add_dependencies(color_correction_client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(color_correction_client
  ${catkin_LIBRARIES}
)

## MCU communication tester
add_executable(mcu_communication_tester src/mcu_communication_tester.cpp)
add_dependencies(mcu_communication_tester ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(mcu_communication_tester
  mcu_communication
  ${catkin_LIBRARIES}
)

## Standalone executable (original main) - optional for backward compatibility
add_executable(color_correction_standalone src/main.cpp)
add_dependencies(color_correction_standalone ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(color_correction_standalone
  ${PROJECT_NAME}
  ${catkin_LIBRARIES}
  ${OpenCV_LIBS}
)
